package org.niux.AutoLoginQQ.ui;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.URLEncoder;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Timer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuDetectEvent;
import org.eclipse.swt.events.MenuDetectListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
import org.eclipse.wb.swt.SWTResourceManager;
import org.niux.AutoLoginQQ.VO.QQNumVO;
import org.niux.AutoLoginQQ.VO.TableVO;
import org.niux.AutoLoginQQ.common.Constant;
import org.niux.AutoLoginQQ.common.ThreadPoolManager;
import org.niux.AutoLoginQQ.operate.AppOperate;
import org.niux.AutoLoginQQ.operate.CheckVersion;
import org.niux.AutoLoginQQ.operate.LoginQQ;
import org.niux.AutoLoginQQ.reg.Reg;
import org.niux.AutoLoginQQ.timer.LogoutTimer;
import org.niux.AutoLoginQQ.timer.ScanDateTimer;
import org.niux.AutoLoginQQ.timer.ScanTaskTimer;
import org.niux.AutoLoginQQ.timer.SendPollTimer;
import org.niux.AutoLoginQQ.timer.TimingLoginTimer;
import org.niux.AutoLoginQQ.util.ConfigurationFile;
import org.niux.AutoLoginQQ.util.FileOperate;
import org.niux.AutoLoginQQ.util.ReadQQList;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Main {

	protected Shell shlNiuxqq;
	private Table table;
	private Main gui = this;
	private ScanTaskTimer taskTimer;
	private SendPollTimer sendPollTimer;
	private ScanDateTimer scanDateTimer;
	private TimingLoginTimer timingLoginTimer;
	private LogoutTimer logoutTimer;
	private Timer timer1;
	private Timer timer2;
	private Timer timer3;
	private Timer timer4;
	private Timer timer5;
	private Label qqCount;
	private Label alreadyLogin;
	private Label wrongPassword;
	private Label failCount;
	private Label waitingLogin;
//	private ServerSocket ss;
	private ToolItem selectTool;
	private ToolBar toolBar;
	private Menu selectMenu;
	private boolean isEncode;
	private TableColumn tblclmnQq;
	private TableColumn tableColumn_1;
	private TableColumn tableColumn_2;
	private TableColumn tableColumn_3;
	private TableColumn tableColumn_4;
	private TableColumn tableColumn_7;

	private String qqNumSort = null;
	private String statusSort = null;
	private String levelSort = null;
	private String onlineSort = null;
	private String remainSort = null;
	private String loginTimeSort = null;

	private Log log = LogFactory.getLog(this.getClass());
	private Text verifyCode;
	private Label verifyCodeImage;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			 Thread.sleep(5000);
//			Thread.sleep(0);

			Main window = new Main();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * 方法概述：开启socket服务
	 * 描述：开启socket服务
	 * 返回类型：void
	 * 修改人：梁铭显 
	 * 修改时间：2013-9-10
	 */
	public void singleton(){
		try{
			File file = new File("c:/windows/system32/lockFile.dll");
			RandomAccessFile raf = new RandomAccessFile(file, "rw");
			FileChannel fc = raf.getChannel();
			FileLock fl = fc.tryLock();
			
			if (!fl.isValid()) {
				alert("您已运行一个NiuXQQ群登器。");
				System.exit(0);
			}
				
		} catch (Exception e) {
			e.printStackTrace();
			alert("您已运行一个NiuXQQ群登器。");
			System.exit(0);
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		log.debug("程序开始运行");
		
		Display display = Display.getDefault();
		createContents();

		shlNiuxqq.open();
		shlNiuxqq.layout();

		isEncode = true;
		
		// 启动Socket服务(限制单开)
		singleton();
//		Constant.initPorts();
//		int failsCount = 0;
//		
//		for (Integer port : Constant.PORTS) {
//			boolean rs = openSocketServer(port);
//			if(rs){
//				break;
//			} else {
//				failsCount++;
//			}
//		}
//		
//		if(failsCount == Constant.PORTS.size()){
//			alert("您已运行一个NiuXQQ群登器，或者您的系统限制启动63512端口");
//			System.exit(0);
//		}
		
//		try {
//			 ss = new ServerSocket(63512);
//		} catch (Exception e) {
//			if (e.getMessage().contains("JVM_Bind")) {
//				alert("您已运行一个NiuXQQ群登器，或者您的系统限制启动63512端口");
//				try {
//					ss.close();
//				} catch (IOException e1) {
//					e1.printStackTrace();
//				}
//				System.exit(0);
//			}
//			e.printStackTrace();
//		}

		Constant.THREAD_POOL_MANAGER = ThreadPoolManager.getInstance(500);

		final Tray tray = display.getSystemTray();

		final TrayItem trayItem = new TrayItem(tray, SWT.NONE);

		trayItem.setToolTipText("NiuXQQ\u7FA4\u767B\u5668 " + Constant.VERSION);

		InputStream is = this.getClass().getResourceAsStream("icon/ico.png");
		Image image = new Image(Display.getDefault(), is);
		trayItem.setImage(image);
		
		trayItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				shlNiuxqq.setVisible(!shlNiuxqq.isVisible());
				if (shlNiuxqq.isVisible()) {
					shlNiuxqq.setMinimized(false); // 阻止窗口最小化
					shlNiuxqq.setActive(); // 激活窗口
					shlNiuxqq.setFocus();// 没有此句可能在其他窗口下面
				}
			}
		});
		
		final Menu trayMenu = new Menu(shlNiuxqq, SWT.POP_UP);
		
		MenuItem aboutItem = new MenuItem(trayMenu, SWT.PUSH);
		
        aboutItem.setText("退出");
        
        aboutItem.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				shlNiuxqq.close();
			}
		});
        
        //系统托盘菜单
		trayItem.addMenuDetectListener(new MenuDetectListener() {
			@Override
			public void menuDetected(MenuDetectEvent arg0) {
				trayMenu.setVisible(true);
			}
		});

		// 通过服务器获取版 本可用性
		// 执行登录操作
		Thread t = new Thread(new Runnable() {
			public void run() {
				CheckVersion chk = new CheckVersion();

				Reg reg = new Reg();
				String guid = reg.getGUID();
				
				if(guid.equals("")){
					Display.getDefault().asyncExec(new Runnable() {
						public void run() {
							alert("机器码获取错误，请与作者联系！");
							tray.dispose();
							System.exit(0);
						}
					});
				}
				
				log.debug(guid);
				
				String response = chk.checkVersion(guid);

				if (!response.equals("") && !response.equals("false")) {
					final String[] resArray = response.split("\\|");
					Display.getDefault().asyncExec(new Runnable() {
						public void run() {
							SuggestDialog suggest = new SuggestDialog(shlNiuxqq, SWT.NONE,
									resArray[0], resArray[1], resArray[2]);
							suggest.open();
						}
					});
				} else if (response.equals("false")) {
					Display.getDefault().asyncExec(new Runnable() {
						public void run() {
							alert("版本验证错误，请与作者联系！");
							tray.dispose();
							System.exit(0);
						}
					});
				}
			}
		});

		t.start();

		Constant.THREAD_POOL.add(t);
		
		// 读取Cookies数据
		File file = new File(System.getProperty("user.dir") + "/Data.CAD");

		if (file.exists()) {
			long modifyTime = file.lastModified();
			if ((System.currentTimeMillis() - modifyTime) < 2 * 60 * 60 * 1000) {
				FileOperate fo = new FileOperate();
				fo.readCookies(file);
			}
		}

		// 判断是否能免码登录
		if (!Constant.LOGIN_DATA.isEmpty()) {
			checkVerifyCode();
		}

		// 读取QQ列表
		File file2 = new File(System.getProperty("user.dir") + "/Data2.CAD");
		
		if (file2.exists()) {
			ReadQQList readQQList = new ReadQQList();

			List<QQNumVO> qqList = readQQList.getQQRecord(System.getProperty("user.dir")
					+ "/Data2.CAD");

			for (QQNumVO qqNumVO : qqList) {
				Constant.QQ_LIST.put(qqNumVO.getQqNum(), qqNumVO);
			}
			
			updateTable(qqList);
			
			updateQQCount(Integer.toString(Constant.QQ_LIST.size()));
		}

		// 启动登录轮循器
		timer1 = new Timer();
		taskTimer = new ScanTaskTimer(gui);
		timer1.schedule(taskTimer, 1000, 2000);

		// 启动发送心跳包轮循器
		timer2 = new Timer();
		sendPollTimer = new SendPollTimer(gui);
		timer2.schedule(sendPollTimer, 5000, 10000);

		// 启动退出轮循器
		timer3 = new Timer();
		logoutTimer = new LogoutTimer(gui);
		timer3.schedule(logoutTimer, 10000, 2000);

		// 日期监控轮循器
		timer4 = new Timer();
		scanDateTimer = new ScanDateTimer(gui);
		timer4.schedule(scanDateTimer, 1000, 30000);
		
		// 启动定时登录器
		if (Constant.TIMING_LOGIN) {
			log.debug("启动定时登录器");
			timer5 = new Timer();
			timingLoginTimer = new TimingLoginTimer(gui, getAllQQ());

			Calendar calendar = Calendar.getInstance();
			Calendar setterTime = Calendar.getInstance();

			setterTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(Constant.HOUR));
			setterTime.set(Calendar.MINUTE, Integer.parseInt(Constant.MINUTE));
			setterTime.set(Calendar.SECOND, 0);

			boolean isAfterSettingTime = calendar.after(setterTime);

			if (isAfterSettingTime) {// 如果当时时间超过了设置的定时登录时间，则设定定时登录器启动时间为下一天的指定时间
				log.debug("设置为下一天");
				calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + 1);
				calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(Constant.HOUR));
				calendar.set(Calendar.MINUTE, Integer.parseInt(Constant.MINUTE));
				calendar.set(Calendar.SECOND, 0);
			} else {
				log.debug("设置为当天");
				calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(Constant.HOUR));
				calendar.set(Calendar.MINUTE, Integer.parseInt(Constant.MINUTE));
				calendar.set(Calendar.SECOND, 0);
			}

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			log.debug("设置定时登录器的启动时间为" + sdf.format(calendar.getTime()));

			timer5.schedule(timingLoginTimer, calendar.getTime(), 24 * 60 * 60 * 1000);
		}

		// 退出程序
		shlNiuxqq.addShellListener(new ShellAdapter() {
			@SuppressWarnings("deprecation")
			public void shellClosed(ShellEvent e) {

				if (!confirm("是否退出NIUXQQ登录器？")) {
					e.doit = false;
					return;
				}

				// 保存登录数据
				File file = new File(System.getProperty("user.dir") + "/Data.CAD");

				if (file.exists()) {
					FileOperate fo = new FileOperate();
					fo.writeCookies(file, Constant.LOGIN_DATA);
				} else {
					try {
						if (!file.createNewFile()) {
							alert("程序错误，请与作者联系");
						} else {
							FileOperate fo = new FileOperate();
							fo.writeCookies(file, Constant.LOGIN_DATA);
						}
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}

				// 保存QQ列表数据
				File file2 = new File(System.getProperty("user.dir") + "/Data2.CAD");

				ReadQQList readQQList = new ReadQQList();

				if (file2.exists()) {
					List<QQNumVO> qqList = new ArrayList<QQNumVO>();
					
					int count = Constant.TABLE_MAPPING.size(); //记录总数
					
					Set<String> keySet = Constant.TABLE_MAPPING.keySet();
					
					for(int i = 1; i <= count; i++){
						for (String s : keySet) {
							if(Constant.TABLE_MAPPING.get(s).getIndex().equals(Integer.toString(i))){
								QQNumVO qq = new QQNumVO();
								qq.setQqNum(Constant.QQ_LIST.get(s).getQqNum());
								qq.setPassword(Constant.QQ_LIST.get(s).getPassword());
								qq.setNick(Constant.QQ_LIST.get(s).getNick());
								qq.setLevel(Constant.QQ_LIST.get(s).getLevel());
								qq.setOnlineDate(Constant.QQ_LIST.get(s).getOnlineDate());
								qq.setRemainDate(Constant.QQ_LIST.get(s).getRemainDate());
								if (Constant.QQ_LIST.get(s).isLogin()) {
									qq.setAlreadyLogin(System.currentTimeMillis()
											- Constant.QQ_LIST.get(s).getLoginTime()
											+ (Constant.QQ_LIST.get(s).getAlreadyLogin() == null ? 0
													: Constant.QQ_LIST.get(s)
															.getAlreadyLogin()));
								} else {
									qq.setAlreadyLogin(Constant.QQ_LIST.get(s)
											.getAlreadyLogin());
								}
								qq.setLoginDate(Constant.QQ_LIST.get(s).getLoginDate());
								qqList.add(qq);
							}
						}
					}

					readQQList.writeQQList(file2, qqList);
				} else {
					try {
						if (!file2.createNewFile()) {
							alert("程序错误，请与作者联系");
						} else {
							List<QQNumVO> qqList = new ArrayList<QQNumVO>();

							int count = Constant.TABLE_MAPPING.size(); //记录总数
							
							Set<String> keySet = Constant.TABLE_MAPPING.keySet();
							
							for(int i = 1; i <= count; i++){
								for (String s : keySet) {
									if(Constant.TABLE_MAPPING.get(s).getIndex().equals(Integer.toString(i))){
										QQNumVO qq = new QQNumVO();
										qq.setQqNum(Constant.QQ_LIST.get(s).getQqNum());
										qq.setPassword(Constant.QQ_LIST.get(s).getPassword());
										qqList.add(qq);
									}
								}
							}

							readQQList.writeQQList(file2, qqList);
						}
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}

				log.debug("共有" + Constant.THREAD_POOL.size() + "条线程");

				for (Thread t : Constant.THREAD_POOL) {
					t.stop();
				}

				taskTimer.cancel();
				sendPollTimer.cancel();
				logoutTimer.cancel();

				timer1.cancel();
				timer2.cancel();
				timer3.cancel();
				timer4.cancel();
				if (timer5 != null) {
					timer5.cancel();
				}

				Display.getDefault().dispose();// 释放底层的资源

				System.exit(1);// 退出主程序
			}

		});

		shlNiuxqq.addShellListener(new ShellAdapter() {
			// 最小化
			public void shellIconified(ShellEvent e) {
				shlNiuxqq.setVisible(!shlNiuxqq.isVisible());// 控制窗口顯示
			}
		});

		// 启动自动登录
		if (Constant.AUTO_LOGIN) {
			final List<QQNumVO> qqList = new ArrayList<QQNumVO>();

			Set<String> keySet = Constant.TABLE_MAPPING.keySet();
			
			for (String s : keySet) {
				Constant.TABLE_MAPPING.get(s).setChecked(true);
				
				QQNumVO qqNumVO = Constant.QQ_LIST.get(s); // 从选择的行中取得QQ号码
				
				// 如果已经登录就过滤掉
				if (qqNumVO.isLogin()) {
					continue;
				}

				qqNumVO.setIndex(Integer.parseInt(Constant.TABLE_MAPPING.get(s).getIndex()) - 1);
				qqList.add(qqNumVO);
			}

			// 执行登录操作
			Thread autoLoginThread = new Thread(new Runnable() {
				public void run() {
					LoginQQ loginQQ = new LoginQQ(gui);

					Constant.SCAN_TASK_TIMER_LOGGER = true;

					loginQQ.login(qqList);

					Constant.SCAN_TASK_TIMER_LOGGER = false;

					if (Constant.AUTO_SHUTDOWN) {
						Constant.SHUTDOWN = true;
					}
				}
			});

			autoLoginThread.start();

			Constant.THREAD_POOL.add(autoLoginThread);
		}

		while (!shlNiuxqq.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}

		tray.dispose();
	}

	/**
	 * Create contents of the window.
	 */
	@SuppressWarnings("static-access")
	protected void createContents() {

		// 读取配置文件
		ConfigurationFile config = new ConfigurationFile();

		String file = "config.ini";

		try {
			// Constant.VERSION = config.getProfileString(file, "Version",
			// "Version", ""); //版本号
			Constant.DELAY_TIME = config.getProfileString(file, "Config", "DelayTime", ""); // 登录间隔时间
			Constant.LOGIN_STATUS = config.getProfileString(file, "Config", "LoginStatus", ""); // 登录状态，40是正常登录，41是隐身登录
			Constant.TOP_LEVEL = config.getProfileString(file, "Config", "TopLevel", "");// 挂Q上限等级
			Constant.SPLIT_STR = config.getProfileString(file, "Config", "SplitStr", "");// QQ列表分割符
			Constant.SAME_TIME_LOGIN = config.getProfileString(file, "Config", "SameTimeLogin", "");// 同时登录个数
			Constant.IS_LOGIN_NEED_VERIFYCODE = config.getProfileString(file, "Config",
					"IsLoginNeedVerifyCode", ""); // 需要验证码时是否登录
			Constant.LOGIN_TIME = config.getProfileString(file, "Config", "LoginTime", ""); // 持续登录时间
			Constant.AUTO_SHUTDOWN = config.getProfileString(file, "Config", "AutoShutdown", "")
					.trim().equals("true"); // 是否自动关机
			Constant.AUTO_RUN = config.getProfileString(file, "Config", "AutoRun", "").trim()
					.equals("true"); // 是否自动启动
			Constant.AUTO_LOGIN = config.getProfileString(file, "Config", "AutoLogin", "").trim()
					.equals("true"); // 是否自动登录
			Constant.SEIZE = config.getProfileString(file, "Config", "Seize", "").trim()
					.equals("true"); // 是否自动抢线
			Constant.TIMING_LOGIN = config.getProfileString(file, "Config", "TimingLogin", "")
					.trim().equals("true"); // 是否定时登录
			Constant.HOUR = config.getProfileString(file, "Config", "Hour", ""); // 小时
			Constant.MINUTE = config.getProfileString(file, "Config", "Minute", ""); // 分钟
		} catch (IOException e1) {
			e1.printStackTrace();
			alert("配置文件读取错误，请重新安装软件！");
			shlNiuxqq.close();
		}

		AppOperate appOperate = new AppOperate();

		// 设置自启动
		appOperate.setAutoRun(Constant.AUTO_RUN);

		shlNiuxqq = new Shell(SWT.CLOSE | SWT.MIN | SWT.APPLICATION_MODAL);

		InputStream is = this.getClass().getResourceAsStream("icon/ico.png");
		Image image = new Image(Display.getDefault(), is);
		shlNiuxqq.setImage(image);

		// 窗口居中
		int width = shlNiuxqq.getMonitor().getClientArea().width;
		int height = shlNiuxqq.getMonitor().getClientArea().height;
		int x = shlNiuxqq.getSize().x;
		int y = shlNiuxqq.getSize().y;
		if (x > width) {
			shlNiuxqq.getSize().x = width;
		}

		if (y > height) {
			shlNiuxqq.getSize().y = height;
		}

		shlNiuxqq.setLocation((width - x) / 2, (height - y) / 2);

		shlNiuxqq.setBackground(SWTResourceManager.getColor(173, 216, 230));
		shlNiuxqq.setSize(942, 587);
		shlNiuxqq.setText("NiuXQQ\u7FA4\u767B\u5668 " + Constant.VERSION);

		// 添加快捷键
		Display.getDefault().addFilter(SWT.KeyDown, new Listener() {

			@Override
			public void handleEvent(Event e) {
				if ((e.stateMask == SWT.CTRL) && (e.keyCode == 'a')) {
					if (table.getItemCount() > 0) {
						Set<String> keySet = Constant.TABLE_MAPPING.keySet();
						
						for (String s : keySet) {
							Constant.TABLE_MAPPING.get(s).setChecked(true);
						}
						
						table.removeAll();
						updateTableNO();
						
						table.selectAll();
					}
				}

				if ((e.stateMask == SWT.CTRL) && (e.keyCode == 'd')) {
					if (table.getItemCount() > 0) {
						boolean flag = confirm("确定删除所选QQ号码？（只能删除未登录的QQ）");

						if (flag) {
							int[] selectIndexs = table.getSelectionIndices();

							for (int i : selectIndexs) {
								for (QQNumVO qq : Constant.ALREADY_LOGIN_QUEEN) {
									if (qq.getQqNum().equals(table.getItem(i).getText(2))) {
										// alert("请先退出QQ后再删除");
										return;
									}
								}
								Constant.QQ_LIST.remove(table.getItem(i).getText(2));
								Constant.TABLE_MAPPING.remove(table.getItem(i).getText(2));
							}
							
							table.removeAll();
							
							updateTableNO();
							
							setSelectItem(selectIndexs[0]);

							updateQQCount(Integer.toString(Constant.QQ_LIST.size()));
						}
					}
				}
			}
		});

		table = new Table(shlNiuxqq, SWT.BORDER | SWT.CHECK | SWT.FULL_SELECTION | SWT.MULTI | SWT.VIRTUAL);

		table.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event event) {
				TableItem item = (TableItem) event.item;
				if(item.getChecked()){
					Constant.TABLE_MAPPING.get(item.getText(2)).setChecked(true);
				} else {
					Constant.TABLE_MAPPING.get(item.getText(2)).setChecked(false);
				}
			}
		});
		
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {

				if (table.getItemCount() == 0) {
					return;
				}

				int selectItem = table.getSelectionIndex(); // 获取选择的号码行数

				table.getItem(selectItem).setChecked(true);
				Constant.TABLE_MAPPING.get(table.getItem(selectItem).getText(2)).setChecked(true);

				final List<QQNumVO> qqList = new ArrayList<QQNumVO>();

				QQNumVO qqNumVO = Constant.QQ_LIST.get(table.getItem(selectItem).getText(2)); // 从选择的行中取得QQ号码
				qqNumVO.setIndex(selectItem);
				qqList.add(qqNumVO);

				// 执行登录操作
				Thread t = new Thread(new Runnable() {
					public void run() {
						Constant.LOOP = true;

						LoginQQ loginQQ = new LoginQQ(gui);

						Constant.SCAN_TASK_TIMER_LOGGER = true;

						loginQQ.login(qqList);

						Constant.SCAN_TASK_TIMER_LOGGER = false;

						if (Constant.AUTO_SHUTDOWN) {
							Constant.SHUTDOWN = true;
						}
					}
				});

				t.start();

				Constant.THREAD_POOL.add(t);
			}

			// 添加右键菜单
			@Override
			public void mouseDown(MouseEvent e) {
				if (table.getItemCount() == 0) {
					return;
				}

				if (e.button == 3) { // 右键
					Menu menu = new Menu(table);
					table.setMenu(menu);

					MenuItem selectItem = new MenuItem(menu, SWT.PUSH);
					selectItem.setText("选择所选号码");

					selectItem.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent e) {
							TableItem[] items = table.getSelection();

							for (TableItem tableItem : items) {
								if (!tableItem.getChecked()) {
									tableItem.setChecked(true);
									Constant.TABLE_MAPPING.get(tableItem.getText(2)).setChecked(true);
								}
							}
						}
					});

					MenuItem unSelectItem = new MenuItem(menu, SWT.PUSH);
					unSelectItem.setText("取消选择所选");

					unSelectItem.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent e) {
							TableItem[] items = table.getSelection();

							for (TableItem tableItem : items) {
								if (tableItem.getChecked()) {
									tableItem.setChecked(false);
									Constant.TABLE_MAPPING.get(tableItem.getText(2)).setChecked(false);
								}
							}
						}
					});

					MenuItem selectAllItem = new MenuItem(menu, SWT.PUSH);
					selectAllItem.setText("全选(&A)\tCtrl+A");

					selectAllItem.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent e) {
							Set<String> keySet = Constant.TABLE_MAPPING.keySet();
							
							for (String s : keySet) {
								Constant.TABLE_MAPPING.get(s).setChecked(true);
							}
							
							table.removeAll();
							updateTableNO();
						}
					});

					InputStream is = this.getClass().getResourceAsStream("icon/selectAll_16.png");
					selectAllItem.setImage(new Image(Display.getDefault(), is));

					MenuItem noneSelectItem = new MenuItem(menu, SWT.PUSH);
					noneSelectItem.setText("取消全选");

					noneSelectItem.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent e) {
							Set<String> keySet = Constant.TABLE_MAPPING.keySet();
							
							for (String s : keySet) {
								Constant.TABLE_MAPPING.get(s).setChecked(false);
							}
							
							table.removeAll();
							updateTableNO();
						}
					});

					is = this.getClass().getResourceAsStream("icon/unSelect_16.png");
					noneSelectItem.setImage(new Image(Display.getDefault(), is));

					new MenuItem(menu, SWT.SEPARATOR);

					MenuItem del = new MenuItem(menu, SWT.PUSH);
					del.setText("删除QQ号码(&D)\tCtrl+D");
					is = this.getClass().getResourceAsStream("icon/deleteQQ_16.png");
					del.setImage(new Image(Display.getDefault(), is));

					del.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent e) {

							boolean flag = confirm("确定删除所选QQ号码？（只能删除未登录的QQ）");

							if (flag) {
								int[] selectIndexs = table.getSelectionIndices();

								for (int i : selectIndexs) {
									for (QQNumVO qq : Constant.ALREADY_LOGIN_QUEEN) {
										if (qq.getQqNum().equals(table.getItem(i).getText(2))) {
											return;
										}
									}
									Constant.QQ_LIST.remove(table.getItem(i).getText(2));
									Constant.TABLE_MAPPING.remove(table.getItem(i).getText(2));
								}

								table.removeAll();

								updateTableNO();

								setSelectItem(selectIndexs[0]);

								updateQQCount(Integer.toString(Constant.QQ_LIST.size()));
							}
						}
					});

					MenuItem login = new MenuItem(menu, SWT.PUSH);
					login.setText("登录QQ");
					is = this.getClass().getResourceAsStream("icon/login_16.png");
					login.setImage(new Image(Display.getDefault(), is));

					login.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent e) {
							final List<QQNumVO> qqList = new ArrayList<QQNumVO>();

							int[] selectIndex = table.getSelectionIndices();

							for (int i = 0; i < table.getSelectionCount(); i++) {
								if (!table.getItem(selectIndex[i]).getChecked()) {
									table.getItem(selectIndex[i]).setChecked(true);
									Constant.TABLE_MAPPING.get(table
										.getItem(selectIndex[i]).getText(2)).setChecked(true);
								}

								QQNumVO qqNumVO = Constant.QQ_LIST.get(table
										.getItem(selectIndex[i]).getText(2)); // 从选择的行中取得QQ号码

								if (qqNumVO.isLogin()) {
									continue;
								}

								qqNumVO.setIndex(selectIndex[i]);
								qqList.add(qqNumVO);
							}

							if (qqList.size() == 0) {
								alert("所选的QQ已经登录");
							}

							// 执行登录操作
							Thread t = new Thread(new Runnable() {
								public void run() {
									if (qqList.size() != 0) {
										Constant.LOOP = true;

										LoginQQ loginQQ = new LoginQQ(gui);

										Constant.SCAN_TASK_TIMER_LOGGER = true;

										loginQQ.login(qqList);

										Constant.SCAN_TASK_TIMER_LOGGER = false;

										if (Constant.AUTO_SHUTDOWN) {
											Constant.SHUTDOWN = true;
										}
									}
								}
							});

							t.start();

							Constant.THREAD_POOL.add(t);
						}
					});

					MenuItem logout = new MenuItem(menu, SWT.PUSH);
					logout.setText("退出登录");
					is = this.getClass().getResourceAsStream("icon/logout_16.png");
					logout.setImage(new Image(Display.getDefault(), is));

					logout.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent e) {
							// 执行退出操作
							Thread t = new Thread(new Runnable() {
								public void run() {
									Display.getDefault().asyncExec(new Runnable() {
										public void run() {
											LoginQQ loginQQ = new LoginQQ(gui);

											TableItem[] items = table.getSelection();

											for (TableItem tableItem : items) {
												QQNumVO qq = Constant.QQ_LIST.get(tableItem
														.getText(2));

												if (qq.isLogin()) {
													loginQQ.addLogoutQueen(qq.getQqNum(), "退出登录"); // 退出登录
												}
											}
										}
									});
								}
							});

							t.start();

							Constant.THREAD_POOL.add(t);
						}
					});

					MenuItem modifyPassword = new MenuItem(menu, SWT.PUSH);
					modifyPassword.setText("修改登录密码");
					is = this.getClass().getResourceAsStream("icon/modifyPassword.png");
					modifyPassword.setImage(new Image(Display.getDefault(), is));

					modifyPassword.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent e) {
							int index = table.getSelectionIndex();
							TableItem item = table.getItem(index);
							String qqNumString = item.getText(2);
							ModifyPasswordDialog mpd = new ModifyPasswordDialog(shlNiuxqq,
									SWT.NONE, qqNumString, gui);
							mpd.open();
						}
					});

					MenuItem modifyUserName = new MenuItem(menu, SWT.PUSH);
					modifyUserName.setText("修改昵称和个性签名");
					is = this.getClass().getResourceAsStream("icon/modify_16.png");
					modifyUserName.setImage(new Image(Display.getDefault(), is));

					modifyUserName.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent e) {
							// 执行修改昵称和个性签名操作
							Thread t = new Thread(new Runnable() {
								public void run() {
									Display.getDefault().asyncExec(new Runnable() {
										public void run() {
											ModifyInfo modify = new ModifyInfo(shlNiuxqq, SWT.NONE);
											modify.open();

											LoginQQ loginQQ = new LoginQQ(gui);

											TableItem[] items = table.getSelection();

											List<String> qqNums = new ArrayList<String>();

											for (TableItem tableItem : items) {
												qqNums.add(tableItem.getText(2));
											}

											loginQQ.modifyNickAndSingleLongNick(modify.getNick(),
													modify.getSingleLongNick(), qqNums);
										}
									});
								}
							});

							t.start();

							Constant.THREAD_POOL.add(t);
						}
					});
				}
			}
		});
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		table.setBounds(10, 80, 916, 352);

		TableColumn tableColumn = new TableColumn(table, SWT.CENTER);
		tableColumn.setWidth(40);
		tableColumn.setText("\u9009\u62E9");

		TableColumn tableColumn_6 = new TableColumn(table, SWT.CENTER);
		tableColumn_6.setWidth(45);
		tableColumn_6.setText("序号");

		tblclmnQq = new TableColumn(table, SWT.CENTER);
		tblclmnQq.setWidth(98);
		tblclmnQq.setText("QQ\u53F7\u7801");

		tblclmnQq.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event arg0) {

				if (qqNumSort == null) {
					qqNumSort = "ASC";
					InputStream is = this.getClass().getResourceAsStream("icon/asc.png");
					tblclmnQq.setImage(new Image(Display.getDefault(), is));
				} else if (qqNumSort == "ASC") {
					qqNumSort = "DESC";
					InputStream is = this.getClass().getResourceAsStream("icon/desc.png");
					tblclmnQq.setImage(new Image(Display.getDefault(), is));
				} else if (qqNumSort == "DESC") {
					qqNumSort = "ASC";
					InputStream is = this.getClass().getResourceAsStream("icon/asc.png");
					tblclmnQq.setImage(new Image(Display.getDefault(), is));
				}

				Thread t = new Thread(new Runnable() {
					public void run() {
						Display.getDefault().asyncExec(new Runnable() {
							public void run() {
								sort(2, qqNumSort);
							}
						});
					}
				});

				t.start();

				Constant.THREAD_POOL.add(t);
			}
		});

		TableColumn tableColumn_8 = new TableColumn(table, SWT.CENTER);
		tableColumn_8.setWidth(62);
		tableColumn_8.setText("密码");

		tableColumn_1 = new TableColumn(table, SWT.CENTER);
		tableColumn_1.setWidth(105);
		tableColumn_1.setText("\u767B\u5F55\u72B6\u6001");

		tableColumn_1.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event arg0) {

				if (statusSort == null) {
					statusSort = "ASC";
					InputStream is = this.getClass().getResourceAsStream("icon/asc.png");
					tableColumn_1.setImage(new Image(Display.getDefault(), is));
				} else if (statusSort == "ASC") {
					statusSort = "DESC";
					InputStream is = this.getClass().getResourceAsStream("icon/desc.png");
					tableColumn_1.setImage(new Image(Display.getDefault(), is));
				} else if (statusSort == "DESC") {
					statusSort = "ASC";
					InputStream is = this.getClass().getResourceAsStream("icon/asc.png");
					tableColumn_1.setImage(new Image(Display.getDefault(), is));
				}

				Thread t = new Thread(new Runnable() {
					public void run() {
						Display.getDefault().asyncExec(new Runnable() {
							public void run() {
								sort(4, statusSort);
							}
						});
					}
				});

				t.start();

				Constant.THREAD_POOL.add(t);
			}
		});

		TableColumn tableColumn_5 = new TableColumn(table, SWT.CENTER);
		tableColumn_5.setWidth(60);
		tableColumn_5.setText("昵称");

		tableColumn_2 = new TableColumn(table, SWT.CENTER);
		tableColumn_2.setWidth(83);
		tableColumn_2.setText("\u7B49\u7EA7");

		tableColumn_2.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event arg0) {

				if (levelSort == null) {
					levelSort = "ASC";
					InputStream is = this.getClass().getResourceAsStream("icon/asc.png");
					tableColumn_2.setImage(new Image(Display.getDefault(), is));
				} else if (levelSort == "ASC") {
					levelSort = "DESC";
					InputStream is = this.getClass().getResourceAsStream("icon/desc.png");
					tableColumn_2.setImage(new Image(Display.getDefault(), is));
				} else if (levelSort == "DESC") {
					levelSort = "ASC";
					InputStream is = this.getClass().getResourceAsStream("icon/asc.png");
					tableColumn_2.setImage(new Image(Display.getDefault(), is));
				}

				Thread t = new Thread(new Runnable() {
					public void run() {
						Display.getDefault().asyncExec(new Runnable() {
							public void run() {
								sort(6, levelSort);
							}
						});
					}
				});

				t.start();

				Constant.THREAD_POOL.add(t);
			}
		});

		tableColumn_3 = new TableColumn(table, SWT.CENTER);
		tableColumn_3.setWidth(102);
		tableColumn_3.setText("\u6D3B\u8DC3\u5929\u6570");

		tableColumn_3.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event arg0) {

				if (onlineSort == null) {
					onlineSort = "ASC";
					InputStream is = this.getClass().getResourceAsStream("icon/asc.png");
					tableColumn_3.setImage(new Image(Display.getDefault(), is));
				} else if (onlineSort == "ASC") {
					onlineSort = "DESC";
					InputStream is = this.getClass().getResourceAsStream("icon/desc.png");
					tableColumn_3.setImage(new Image(Display.getDefault(), is));
				} else if (onlineSort == "DESC") {
					onlineSort = "ASC";
					InputStream is = this.getClass().getResourceAsStream("icon/asc.png");
					tableColumn_3.setImage(new Image(Display.getDefault(), is));
				}

				Thread t = new Thread(new Runnable() {
					public void run() {
						Display.getDefault().asyncExec(new Runnable() {
							public void run() {
								sort(7, onlineSort);
							}
						});
					}
				});

				t.start();

				Constant.THREAD_POOL.add(t);
			}
		});

		tableColumn_4 = new TableColumn(table, SWT.CENTER);
		tableColumn_4.setWidth(102);
		tableColumn_4.setText("\u5269\u4F59\u5929\u6570");

		tableColumn_4.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event arg0) {

				if (remainSort == null) {
					remainSort = "ASC";
					InputStream is = this.getClass().getResourceAsStream("icon/asc.png");
					tableColumn_4.setImage(new Image(Display.getDefault(), is));
				} else if (remainSort == "ASC") {
					remainSort = "DESC";
					InputStream is = this.getClass().getResourceAsStream("icon/desc.png");
					tableColumn_4.setImage(new Image(Display.getDefault(), is));
				} else if (remainSort == "DESC") {
					remainSort = "ASC";
					InputStream is = this.getClass().getResourceAsStream("icon/asc.png");
					tableColumn_4.setImage(new Image(Display.getDefault(), is));
				}

				Thread t = new Thread(new Runnable() {
					public void run() {
						Display.getDefault().asyncExec(new Runnable() {
							public void run() {
								sort(8, remainSort);
							}
						});
					}
				});

				t.start();

				Constant.THREAD_POOL.add(t);
			}
		});

		tableColumn_7 = new TableColumn(table, SWT.CENTER);
		tableColumn_7.setWidth(106);
		tableColumn_7.setText("挂机时间");

		tableColumn_7.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event arg0) {

				if (loginTimeSort == null) {
					loginTimeSort = "ASC";
					InputStream is = this.getClass().getResourceAsStream("icon/asc.png");
					tableColumn_7.setImage(new Image(Display.getDefault(), is));
				} else if (loginTimeSort == "ASC") {
					loginTimeSort = "DESC";
					InputStream is = this.getClass().getResourceAsStream("icon/desc.png");
					tableColumn_7.setImage(new Image(Display.getDefault(), is));
				} else if (loginTimeSort == "DESC") {
					loginTimeSort = "ASC";
					InputStream is = this.getClass().getResourceAsStream("icon/asc.png");
					tableColumn_7.setImage(new Image(Display.getDefault(), is));
				}

				Thread t = new Thread(new Runnable() {
					public void run() {
						Display.getDefault().asyncExec(new Runnable() {
							public void run() {
								sort(9, loginTimeSort);
							}
						});
					}
				});

				t.start();

				Constant.THREAD_POOL.add(t);
			}
		});

		TableColumn tableColumn_9 = new TableColumn(table, SWT.CENTER);
		tableColumn_9.setWidth(88);
		tableColumn_9.setText("操作状态");

		Group group = new Group(shlNiuxqq, SWT.NONE);
		group.setBackground(SWTResourceManager.getColor(173, 216, 230));
		group.setText("统计信息");
		group.setBounds(10, 438, 762, 56);

		Label lblQq = new Label(group, SWT.NONE);
		lblQq.setBackground(SWTResourceManager.getColor(173, 216, 230));
		lblQq.setBounds(10, 25, 54, 18);
		lblQq.setText("QQ总数：");

		qqCount = new Label(group, SWT.NONE);
		qqCount.setBackground(SWTResourceManager.getColor(173, 216, 230));
		qqCount.setBounds(70, 25, 54, 18);
		qqCount.setText("0");

		Label lblNewLabel = new Label(group, SWT.NONE);
		lblNewLabel.setBackground(SWTResourceManager.getColor(173, 216, 230));
		lblNewLabel.setBounds(130, 25, 72, 18);
		lblNewLabel.setText("已登录个数：");

		alreadyLogin = new Label(group, SWT.NONE);
		alreadyLogin.setBackground(SWTResourceManager.getColor(173, 216, 230));
		alreadyLogin.setBounds(208, 25, 54, 18);
		alreadyLogin.setText("0");

		Label lblNewLabel_1 = new Label(group, SWT.NONE);
		lblNewLabel_1.setBackground(SWTResourceManager.getColor(173, 216, 230));
		lblNewLabel_1.setBounds(418, 25, 84, 18);
		lblNewLabel_1.setText("密码错误个数：");

		wrongPassword = new Label(group, SWT.NONE);
		wrongPassword.setBackground(SWTResourceManager.getColor(173, 216, 230));
		wrongPassword.setBounds(508, 25, 54, 18);
		wrongPassword.setText("0");

		Label lblNewLabel_3 = new Label(group, SWT.NONE);
		lblNewLabel_3.setBackground(SWTResourceManager.getColor(173, 216, 230));
		lblNewLabel_3.setBounds(568, 25, 84, 18);
		lblNewLabel_3.setText("登录失败个数：");

		failCount = new Label(group, SWT.NONE);
		failCount.setBackground(SWTResourceManager.getColor(173, 216, 230));
		failCount.setBounds(658, 25, 31, 18);
		failCount.setText("0");

		Label lblNewLabel_5 = new Label(group, SWT.NONE);
		lblNewLabel_5.setBackground(SWTResourceManager.getColor(173, 216, 230));
		lblNewLabel_5.setBounds(268, 25, 84, 18);
		lblNewLabel_5.setText("等待登录个数：");

		waitingLogin = new Label(group, SWT.NONE);
		waitingLogin.setBackground(SWTResourceManager.getColor(173, 216, 230));
		waitingLogin.setBounds(358, 25, 54, 18);
		waitingLogin.setText("0");

		toolBar = new ToolBar(shlNiuxqq, SWT.FLAT | SWT.WRAP);
		toolBar.setBackground(SWTResourceManager.getColor(173, 216, 230));
		toolBar.setBounds(10, 10, 916, 64);

		ToolItem importTool = new ToolItem(toolBar, SWT.PUSH);
		importTool.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FileDialog dialog = new FileDialog(shlNiuxqq, SWT.NONE);
				dialog.setText("请选择QQ列表文件...");
				dialog.setFilterNames(new String[] { "文本文件", "文本文件 (*.txt)" });
				dialog.setFilterExtensions(new String[] { "*.txt" });
				String path = dialog.open();

				if (path != null) {
					File file = new File(path);

					if (!file.exists()) {
						alert("选择的文件不存在");
						return;
					}

					ReadQQList readQQList = new ReadQQList();
					List<QQNumVO> qqList = readQQList.getQQList(path);

					if (qqList.isEmpty()) {
						alert("读取的QQ号码列表为空，请确定列表文件的分隔符与软件设置中的分隔符一致");
					}
					// Constant.QQ_LIST.clear();

					Iterator<QQNumVO> qi = qqList.iterator();

					while (qi.hasNext()) {
						QQNumVO qqNumVO = qi.next();

						if (Constant.QQ_LIST.containsKey(qqNumVO.getQqNum())) {
							qi.remove();
						} else {
							Constant.QQ_LIST.put(qqNumVO.getQqNum(), qqNumVO);
						}
					}
					//
					// for (QQNumVO qqNumVO : qqList) {
					// Constant.QQ_LIST.put(qqNumVO.getQqNum(), qqNumVO);
					// }

					updateTable(qqList);
					updateQQCount(Integer.toString(Constant.QQ_LIST.size()));
				}
			}
		});
		importTool.setText("导入");
		is = this.getClass().getResourceAsStream("icon/import.png");
		importTool.setImage(new Image(Display.getDefault(), is));
		importTool.setToolTipText("导入QQ列表");

		ToolItem exportTool = new ToolItem(toolBar, SWT.PUSH);
		exportTool.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (table.getItemCount() == 0) {
					alert("没有需要导出的QQ列表");
					return;
				}

				ExportSetter export = new ExportSetter(shlNiuxqq, SWT.NONE, table);

				export.open();
			}
		});
		exportTool.setText("导出");
		is = this.getClass().getResourceAsStream("icon/export.png");
		exportTool.setImage(new Image(Display.getDefault(), is));
		exportTool.setToolTipText("导出QQ列表");

		ToolItem loginTool = new ToolItem(toolBar, SWT.PUSH);
		loginTool.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {

				Constant.LOOP = true;

				int count = table.getItemCount();

				if (count == 0) {
					alert("请先导入QQ列表！");
				} else {
					int selectCount = 0;

					Set<String> keySet = Constant.TABLE_MAPPING.keySet();
					
					for (String s : keySet) {
						if(Constant.TABLE_MAPPING.get(s).isChecked()){
							selectCount++;
						}
					}

					if (selectCount == 0) {
						alert("请选择需要登录的QQ号码！");
					} else {
						final List<QQNumVO> qqList = new ArrayList<QQNumVO>();

						//表格映射对象排序
						List<QQNumVO> sortList = new ArrayList<QQNumVO>();
						
						Set<String> qqNums = Constant.TABLE_MAPPING.keySet();
						
						for(String qqNum : qqNums){
							QQNumVO tempVo = new QQNumVO();
							
							tempVo.setQqNum(qqNum);
							
							TableVO tableVo = Constant.TABLE_MAPPING.get(qqNum);
							
							tempVo.setSortElementByInteger(Integer.parseInt(tableVo.getIndex()));

							sortList.add(tempVo);
						}
						
						// 使用JAVA6的排序方法
						System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");

						// 排序
						Collections.sort(sortList, new Comparator<QQNumVO>() {
							@Override
							public int compare(QQNumVO o1, QQNumVO o2) {
								return o1.getSortElementByInteger() > o2.getSortElementByInteger() ? 1 : -1;
							}
						});
						
						//更新表格映射对象序号
						for(int i = 0; i < sortList.size(); i++){
							if(Constant.TABLE_MAPPING.get(sortList.get(i).getQqNum()).isChecked()){
								QQNumVO qqNumVO = Constant.QQ_LIST.get(table.getItem(i).getText(2)); // 从选择的行中取得QQ号码

								// 如果已经登录就过滤掉
								if (qqNumVO.isLogin()) {
									continue;
								}

								qqNumVO.setIndex(i);
								qqList.add(qqNumVO);
							}
						}
						
						// 执行登录操作
						Thread t = new Thread(new Runnable() {
							public void run() {
								LoginQQ loginQQ = new LoginQQ(gui);

								Constant.SCAN_TASK_TIMER_LOGGER = true;

								loginQQ.login(qqList);

								Constant.SCAN_TASK_TIMER_LOGGER = false;

								if (Constant.AUTO_SHUTDOWN) {
									Constant.SHUTDOWN = true;
								}
							}
						});

						t.start();

						Constant.THREAD_POOL.add(t);
					}
				}
			}
		});
		loginTool.setText("登录");
		is = this.getClass().getResourceAsStream("icon/login.png");
		loginTool.setImage(new Image(Display.getDefault(), is));
		loginTool.setToolTipText("开始群登");

		ToolItem pauseTool = new ToolItem(toolBar, SWT.PUSH);
		pauseTool.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Constant.LOOP = false;
				Constant.LOGIN_QUEEN.clear();
			}
		});
		pauseTool.setText("暂停");
		is = this.getClass().getResourceAsStream("icon/pause.png");
		pauseTool.setImage(new Image(Display.getDefault(), is));
		pauseTool.setToolTipText("暂停群登");

		new ToolItem(toolBar, SWT.SEPARATOR);

		ToolItem addTool = new ToolItem(toolBar, SWT.PUSH);
		addTool.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				AddQQ addQQ = new AddQQ(shlNiuxqq, SWT.CANCEL, gui);
				addQQ.open();
			}
		});
		addTool.setText("添加");
		is = this.getClass().getResourceAsStream("icon/add.png");
		addTool.setImage(new Image(Display.getDefault(), is));
		addTool.setToolTipText("添加QQ号码");

		ToolItem deleteTool = new ToolItem(toolBar, SWT.PUSH);
		deleteTool.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (table.getItemCount() == 0) {
					alert("列表中没有数据");
					return;
				}

				if (confirm("是否确定删除所选QQ?（只能删除未登录的QQ）")) {
					
					Set<String> keySet = Constant.TABLE_MAPPING.keySet();
					
					List<String> canDel = new ArrayList<String>();
					
					int index = 0;
					
					for(String s : keySet){
						if(Constant.TABLE_MAPPING.get(s).isChecked()){
							boolean flag = true;
							
							for (QQNumVO qq : Constant.ALREADY_LOGIN_QUEEN) {
								if (qq.getQqNum().equals(s)) {
									flag = false;
								}
							}
							
							if(flag){
								canDel.add(s);
							}
						}
					}
					
					for (String s : canDel) {
						index = Constant.QQ_LIST.get(s).getIndex();
						
						Constant.QQ_LIST.remove(s);
						Constant.TABLE_MAPPING.remove(s);
					}

					table.removeAll();
					
					updateTableNO();
					
					setSelectItem(index);

					updateQQCount(Integer.toString(Constant.QQ_LIST.size()));
				}
			}
		});

		deleteTool.setText("删除");
		is = this.getClass().getResourceAsStream("icon/deleteQQ.png");
		deleteTool.setImage(new Image(Display.getDefault(), is));
		deleteTool.setToolTipText("删除QQ");

		ToolItem emptyTool = new ToolItem(toolBar, SWT.PUSH);
		emptyTool.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (table.getItemCount() == 0) {
					alert("列表中没有数据");
					return;
				}

				if (confirm("是否确定清空列表")) {
					if (Constant.ALREADY_LOGIN_QUEEN.size() > 0) {
						alert("请先退出所有QQ后再清空列表");
						return;
					} else {
						Constant.ALREADY_LOGIN_QUEEN.clear(); // 清空已登录队列
						Constant.LOGIN_QUEEN.clear(); // 清空等待登录队列
						Constant.QQ_LIST.clear(); // 清空QQ列表

						table.removeAll();
						
						Constant.TABLE_MAPPING.clear();

						updateQQCount("0");
						
						isEncode = true;
					}
				}
			}
		});
		emptyTool.setText("清空");
		is = this.getClass().getResourceAsStream("icon/delete.png");
		emptyTool.setImage(new Image(Display.getDefault(), is));
		emptyTool.setToolTipText("清空列表");

		ToolItem modifyTool = new ToolItem(toolBar, SWT.PUSH);
		modifyTool.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (Constant.ALREADY_LOGIN_QUEEN.isEmpty()) {
					alert("请先登录QQ");
					return;
				}

				final ModifyInfo modify = new ModifyInfo(shlNiuxqq, SWT.NONE);
				modify.open();

				// 执行修改昵称操作
				Thread t = new Thread(new Runnable() {
					public void run() {
						LoginQQ loginQQ = new LoginQQ(gui);
						loginQQ.modifyNickAndSingleLongNick(modify.getNick(),
								modify.getSingleLongNick(), new ArrayList<String>());
					}
				});

				t.start();

				Constant.THREAD_POOL.add(t);
			}
		});
		modifyTool.setText("修改");
		is = this.getClass().getResourceAsStream("icon/modify.png");
		modifyTool.setImage(new Image(Display.getDefault(), is));
		modifyTool.setToolTipText("修改昵称和个性签名");

		new ToolItem(toolBar, SWT.SEPARATOR);

		selectTool = new ToolItem(toolBar, SWT.DROP_DOWN);
		selectTool.setText("选择");
		is = this.getClass().getResourceAsStream("icon/select.png");
		selectTool.setImage(new Image(Display.getDefault(), is));
		selectTool.setToolTipText("选择");

		selectMenu = new Menu(shlNiuxqq);

		MenuItem selectOnLineItem = new MenuItem(selectMenu, SWT.PUSH);
		selectOnLineItem.setText("选择正常登录的数据");

		selectOnLineItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Set<String> keySet = Constant.TABLE_MAPPING.keySet();
				
				for (String s : keySet) {
					if (Constant.TABLE_MAPPING.get(s).getStatus().contains("在线") || Constant.TABLE_MAPPING.get(s).getStatus().contains("Q我吧")
							|| Constant.TABLE_MAPPING.get(s).getStatus().contains("离开")
							|| Constant.TABLE_MAPPING.get(s).getStatus().contains("忙碌")
							|| Constant.TABLE_MAPPING.get(s).getStatus().contains("请勿打扰")
							|| Constant.TABLE_MAPPING.get(s).getStatus().contains("隐身")) {
						Constant.TABLE_MAPPING.get(s).setChecked(true);
					} else {
						Constant.TABLE_MAPPING.get(s).setChecked(false);
					}
				}
				
				table.removeAll();
				updateTableNO();
			}
		});

		MenuItem selectOffLineItem = new MenuItem(selectMenu, SWT.PUSH);
		selectOffLineItem.setText("选择掉线的数据");

		selectOffLineItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Set<String> keySet = Constant.TABLE_MAPPING.keySet();
				
				for (String s : keySet) {
					if (Constant.TABLE_MAPPING.get(s).getStatus().contains("掉线")) {
						Constant.TABLE_MAPPING.get(s).setChecked(true);
					} else {
						Constant.TABLE_MAPPING.get(s).setChecked(false);
					}
				}
				
				table.removeAll();
				updateTableNO();
			}
		});

		MenuItem selectWrongPasswordItem = new MenuItem(selectMenu, SWT.PUSH);
		selectWrongPasswordItem.setText("选择密码错误的数据");

		selectWrongPasswordItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Set<String> keySet = Constant.TABLE_MAPPING.keySet();
				
				for (String s : keySet) {
					if (Constant.TABLE_MAPPING.get(s).getStatus().contains("密码错误")) {
						Constant.TABLE_MAPPING.get(s).setChecked(true);
					} else {
						Constant.TABLE_MAPPING.get(s).setChecked(false);
					}
				}
				
				table.removeAll();
				updateTableNO();
			}
		});

		MenuItem selectTopLevelItem = new MenuItem(selectMenu, SWT.PUSH);
		selectTopLevelItem.setText("选择满级的数据");

		selectTopLevelItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Set<String> keySet = Constant.TABLE_MAPPING.keySet();
				
				for (String s : keySet) {
					if (Constant.TABLE_MAPPING.get(s).getStatus().contains("已到达等级上限")) {
						Constant.TABLE_MAPPING.get(s).setChecked(true);
					} else {
						Constant.TABLE_MAPPING.get(s).setChecked(false);
					}
				}
				
				table.removeAll();
				updateTableNO();
			}
		});

		MenuItem selectFailLoginItem = new MenuItem(selectMenu, SWT.PUSH);
		selectFailLoginItem.setText("选择登录失败的数据");

		selectFailLoginItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Set<String> keySet = Constant.TABLE_MAPPING.keySet();
				
				for (String s : keySet) {
					if (Constant.TABLE_MAPPING.get(s).getStatus().contains("限制登录")) {
						Constant.TABLE_MAPPING.get(s).setChecked(true);
					} else {
						Constant.TABLE_MAPPING.get(s).setChecked(false);
					}
				}
				
				table.removeAll();
				updateTableNO();
			}
		});

		selectTool.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				if (event.detail == SWT.ARROW) { // 如果触发的事件为单击按钮
					// 获得按钮所在坐标位置和大小
					Rectangle rect = selectTool.getBounds();
					// 计算菜单出现的起始位置
					Point pt = new Point(rect.x, rect.y + rect.height);
					// 将该点转化为与屏幕的相对位置
					pt = toolBar.toDisplay(pt);
					selectMenu.setLocation(pt.x, pt.y); // 设置菜单显示的位置
					selectMenu.setVisible(true);
				}
			}
		});

		ToolItem selectAllTool = new ToolItem(toolBar, SWT.PUSH);
		selectAllTool.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Set<String> keySet = Constant.TABLE_MAPPING.keySet();
				
				for(String s : keySet){
					Constant.TABLE_MAPPING.get(s).setChecked(true);
				}
				
				table.removeAll();
				
				Listener listener = new Listener() {
					@Override
					public void handleEvent(Event event) {
						TableItem item = (TableItem) event.item;
						int index = event.index;
						
						Set<String> qqNums = Constant.TABLE_MAPPING.keySet();
						
						for(String qqNum : qqNums){
							if(Integer.parseInt(Constant.TABLE_MAPPING.get(qqNum).getIndex()) - 1 == index){
								item.setText(new String[] { "", Constant.TABLE_MAPPING.get(qqNum).getIndex(),
										Constant.TABLE_MAPPING.get(qqNum).getNum(), 
										Constant.TABLE_MAPPING.get(qqNum).getPassword(),
										Constant.TABLE_MAPPING.get(qqNum).getStatus(),
										Constant.TABLE_MAPPING.get(qqNum).getNick(),
										Constant.TABLE_MAPPING.get(qqNum).getLevel(), 
										Constant.TABLE_MAPPING.get(qqNum).getOnlineDay(), 
										Constant.TABLE_MAPPING.get(qqNum).getRemainDay(),
										format(Constant.TABLE_MAPPING.get(qqNum).getLoginTime()), 
										Constant.TABLE_MAPPING.get(qqNum).getOpStatus() });
								item.setChecked(true);
								break;
							}
						}
					}
				};
				
				table.addListener(SWT.SetData, listener);
				
				table.setItemCount(Constant.TABLE_MAPPING.size());
			}
		});
		selectAllTool.setText("全选");
		is = this.getClass().getResourceAsStream("icon/selectAll.png");
		selectAllTool.setImage(new Image(Display.getDefault(), is));
		selectAllTool.setToolTipText("全选");

		ToolItem unSelectTool = new ToolItem(toolBar, SWT.PUSH);

		unSelectTool.setText("反选");
		is = this.getClass().getResourceAsStream("icon/unSelect.png");
		unSelectTool.setImage(new Image(Display.getDefault(), is));
		unSelectTool.setToolTipText("反选");

		unSelectTool.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
//				int count = table.getItemCount();
//
//				for (int i = 0; i < count; i++) {
//					if (table.getItem(i).getChecked()) {
//						table.getItem(i).setChecked(false);// 取消选择
//					}
//				}
				
				Set<String> keySet = Constant.TABLE_MAPPING.keySet();
				
				for(String s : keySet){
					Constant.TABLE_MAPPING.get(s).setChecked(false);
				}
				
				table.removeAll();
				
				Listener listener = new Listener() {
					@Override
					public void handleEvent(Event event) {
						TableItem item = (TableItem) event.item;
						int index = event.index;
						
						Set<String> qqNums = Constant.TABLE_MAPPING.keySet();
						
						for(String qqNum : qqNums){
							if(Integer.parseInt(Constant.TABLE_MAPPING.get(qqNum).getIndex()) - 1 == index){
								item.setText(new String[] { "", Constant.TABLE_MAPPING.get(qqNum).getIndex(),
										Constant.TABLE_MAPPING.get(qqNum).getNum(), 
										Constant.TABLE_MAPPING.get(qqNum).getPassword(),
										Constant.TABLE_MAPPING.get(qqNum).getStatus(),
										Constant.TABLE_MAPPING.get(qqNum).getNick(),
										Constant.TABLE_MAPPING.get(qqNum).getLevel(), 
										Constant.TABLE_MAPPING.get(qqNum).getOnlineDay(), 
										Constant.TABLE_MAPPING.get(qqNum).getRemainDay(),
										format(Constant.TABLE_MAPPING.get(qqNum).getLoginTime()), 
										Constant.TABLE_MAPPING.get(qqNum).getOpStatus() });
								item.setChecked(false);
								break;
							}
						}
					}
				};
				
				table.addListener(SWT.SetData, listener);
				
				table.setItemCount(Constant.TABLE_MAPPING.size());
			}
		});

		ToolItem encodeTool = new ToolItem(toolBar, SWT.PUSH);
		encodeTool.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (table.getItemCount() == 0) {
					alert("还没有导入数据");
					isEncode = false;
					return;
				}

				if (isEncode) {
					alert("密码已经加密");
				} else {
					isEncode = true;

					Set<String> qqNums = Constant.TABLE_MAPPING.keySet();
					
					for(String qqNum : qqNums){
						Constant.TABLE_MAPPING.get(qqNum).setPassword(encodePassword(Constant.TABLE_MAPPING.get(qqNum).getPassword()));
					}
					
					table.removeAll();
					
					Listener listener = new Listener() {
						@Override
						public void handleEvent(Event event) {
							TableItem item = (TableItem) event.item;
							int index = event.index;
							
							Set<String> qqNums = Constant.TABLE_MAPPING.keySet();
							
							for(String qqNum : qqNums){
								if(Integer.parseInt(Constant.TABLE_MAPPING.get(qqNum).getIndex()) - 1 == index){
									item.setText(new String[] { "", Constant.TABLE_MAPPING.get(qqNum).getIndex(),
											Constant.TABLE_MAPPING.get(qqNum).getNum(), 
											Constant.TABLE_MAPPING.get(qqNum).getPassword(),
											Constant.TABLE_MAPPING.get(qqNum).getStatus(),
											Constant.TABLE_MAPPING.get(qqNum).getNick(),
											Constant.TABLE_MAPPING.get(qqNum).getLevel(), 
											Constant.TABLE_MAPPING.get(qqNum).getOnlineDay(), 
											Constant.TABLE_MAPPING.get(qqNum).getRemainDay(),
											format(Constant.TABLE_MAPPING.get(qqNum).getLoginTime()), 
											Constant.TABLE_MAPPING.get(qqNum).getOpStatus() });
									break;
								}
							}
						}
					};
					
					table.addListener(SWT.SetData, listener);
					
					table.setItemCount(Constant.TABLE_MAPPING.size());
					
					table.removeListener(SWT.SetData, listener);
				}
			}
		});
		encodeTool.setText("加密");
		is = this.getClass().getResourceAsStream("icon/encode.png");
		encodeTool.setImage(new Image(Display.getDefault(), is));
		encodeTool.setToolTipText("密码加密");

		ToolItem decodeTool = new ToolItem(toolBar, SWT.PUSH);
		decodeTool.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (table.getItemCount() == 0) {
					alert("还没有导入数据");
					isEncode = false;
					return;
				}

				if (!isEncode) {
					alert("密码已经解密");
				} else {
					isEncode = false;
					
					Set<String> qqNums = Constant.TABLE_MAPPING.keySet();
					
					for(String qqNum : qqNums){
						Constant.TABLE_MAPPING.get(qqNum).setPassword(decodePassword(Constant.TABLE_MAPPING.get(qqNum).getPassword()));
					}
					
					table.removeAll();
					
					table.addListener(SWT.SetData, new Listener() {
						@Override
						public void handleEvent(Event event) {
							TableItem item = (TableItem) event.item;
							int index = event.index;
							
							Set<String> qqNums = Constant.TABLE_MAPPING.keySet();
							
							for(String qqNum : qqNums){
								if(Integer.parseInt(Constant.TABLE_MAPPING.get(qqNum).getIndex()) - 1 == index){
									item.setText(new String[] { "", Constant.TABLE_MAPPING.get(qqNum).getIndex(),
											Constant.TABLE_MAPPING.get(qqNum).getNum(), 
											Constant.TABLE_MAPPING.get(qqNum).getPassword(),
											Constant.TABLE_MAPPING.get(qqNum).getStatus(),
											Constant.TABLE_MAPPING.get(qqNum).getNick(),
											Constant.TABLE_MAPPING.get(qqNum).getLevel(), 
											Constant.TABLE_MAPPING.get(qqNum).getOnlineDay(), 
											Constant.TABLE_MAPPING.get(qqNum).getRemainDay(),
											format(Constant.TABLE_MAPPING.get(qqNum).getLoginTime()), 
											Constant.TABLE_MAPPING.get(qqNum).getOpStatus() });
									break;
								}
							}
						}
					});
					
					table.setItemCount(Constant.TABLE_MAPPING.size());
				}
			}
		});
		decodeTool.setText("解密");
		is = this.getClass().getResourceAsStream("icon/decode.png");
		decodeTool.setImage(new Image(Display.getDefault(), is));
		decodeTool.setToolTipText("密码解密");

		new ToolItem(toolBar, SWT.SEPARATOR);

		ToolItem settingTool = new ToolItem(toolBar, SWT.PUSH);
		settingTool.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ConfigDialog config = new ConfigDialog(shlNiuxqq, SWT.CANCEL, gui);
				config.open();
			}
		});
		settingTool.setText("设置");
		is = this.getClass().getResourceAsStream("icon/setting.png");
		settingTool.setImage(new Image(Display.getDefault(), is));
		settingTool.setToolTipText("设置");

		ToolItem aboutTool = new ToolItem(toolBar, SWT.PUSH);
		aboutTool.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				About about = new About(shlNiuxqq, SWT.NONE);
				about.open();
			}
		});
		aboutTool.setText("关于");
		is = this.getClass().getResourceAsStream("icon/about.png");
		aboutTool.setImage(new Image(Display.getDefault(), is));
		aboutTool.setToolTipText("关于我们");

		ToolItem exitTool = new ToolItem(toolBar, SWT.PUSH);
		exitTool.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shlNiuxqq.close();
			}
		});
		exitTool.setText("退出");
		is = this.getClass().getResourceAsStream("icon/exit.png");
		exitTool.setImage(new Image(Display.getDefault(), is));
		exitTool.setToolTipText("退出");

		Group group_1 = new Group(shlNiuxqq, SWT.NONE);
		group_1.setBackground(SWTResourceManager.getColor(173, 216, 230));
		group_1.setText("验证码");
		group_1.setBounds(778, 438, 148, 111);

		verifyCodeImage = new Label(group_1, SWT.NONE);
		verifyCodeImage.setBackground(SWTResourceManager.getColor(173, 216, 230));
		verifyCodeImage.setBounds(10, 20, 130, 53);

		verifyCode = new Text(group_1, SWT.BORDER);
		verifyCode.setBounds(10, 79, 130, 23);

		verifyCode.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				
				if (verifyCode.getText().contains("\\")) {
					verifyCode.setText(verifyCode.getText().replace("\\", ""));
					verifyCode.setSelection(4, 4);
				}
				
				if (verifyCode.getText().contains(" ")) {
					verifyCode.setText(verifyCode.getText().replaceAll(" ", ""));
					verifyCode.setSelection(4, 4);
				}
//				verifyCode.setText(verifyCode.getText().replaceAll(" ", ""));

				if (verifyCode.getText().length() == 4) {
					Constant.VERIFYCODE = verifyCode.getText();
					verifyCode.setText("");
					verifyCodeImage.setImage(null);
				}
			}
		});

		Group group_2 = new Group(shlNiuxqq, SWT.NONE);
		group_2.setBackground(SWTResourceManager.getColor(173, 216, 230));
		group_2.setText("联系信息");
		group_2.setBounds(10, 500, 762, 48);

		Label lblliangmingxiangmailcom = new Label(group_2, SWT.WRAP);
		lblliangmingxiangmailcom.setBounds(10, 22, 742, 16);
		lblliangmingxiangmailcom.setBackground(SWTResourceManager.getColor(173, 216, 230));
		lblliangmingxiangmailcom.setText("作者：梁铭显 邮箱：liangmingxian@gmail.com NiuXQQ交流群：29520349");
	}

	/**
	 * 
	 * 方法概述：将毫秒数换算成x时x分x秒 描述：将毫秒数换算成x时x分x秒
	 * 
	 * @param ms
	 * @return 返回类型：String 修改人：梁铭显 修改时间：2012-9-11
	 */
	public String format(long ms) {// 将毫秒数换算成x时x分x秒
		int ss = 1000;
		int mi = ss * 60;
		int hh = mi * 60;
		int dd = hh * 24;

		long day = ms / dd;
		long hour = (ms - day * dd) / hh;
		long minute = (ms - day * dd - hour * hh) / mi;
		long second = (ms - day * dd - hour * hh - minute * mi) / ss;

		String strHour = hour < 10 ? "0" + hour : "" + hour;
		String strMinute = minute < 10 ? "0" + minute : "" + minute;
		String strSecond = second < 10 ? "0" + second : "" + second;

		return strHour + ":" + strMinute + ":" + strSecond;
	}

	/**
	 * 
	 * 方法概述：更新序号 
	 * 描述：更新序号 
	 * 返回类型：void 
	 * 修改人：梁铭显 
	 * 修改时间：2012-10-18
	 */
	public void updateTableNO() {
		//表格映射对象排序
		List<QQNumVO> sortList = new ArrayList<QQNumVO>();
		
		Set<String> qqNums = Constant.TABLE_MAPPING.keySet();
		
		for(String qqNum : qqNums){
			QQNumVO tempVo = new QQNumVO();
			
			tempVo.setQqNum(qqNum);
			
			TableVO tableVo = Constant.TABLE_MAPPING.get(qqNum);
			
			tempVo.setSortElementByInteger(Integer.parseInt(tableVo.getIndex()));

			sortList.add(tempVo);
		}
		
		// 使用JAVA6的排序方法
		System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");

		// 排序
		Collections.sort(sortList, new Comparator<QQNumVO>() {
			@Override
			public int compare(QQNumVO o1, QQNumVO o2) {
				return o1.getSortElementByInteger() > o2.getSortElementByInteger() ? 1 : -1;
			}
		});
		
		//更新表格映射对象序号
		for(int i = 0; i < sortList.size(); i++){
			Constant.TABLE_MAPPING.get(sortList.get(i).getQqNum()).setIndex(Integer.toString(i + 1));
			Constant.QQ_LIST.get(sortList.get(i).getQqNum()).setIndex(i);
		}
		
//		final int PAGE_SIZE = 15; 
//		
//		Listener listener = new Listener () { 
//			public void handleEvent (Event event) { 
//				TableItem item = (TableItem) event.item; 
//				int index = event.index;
//				
//				int page = index / PAGE_SIZE; 
//				int start = page * PAGE_SIZE; 
//				int end = start + PAGE_SIZE; 
//				end = Math.min (end, table.getItemCount ()); 
//				for (int i = start; i < end; i++) {
//					log.info("加载第" + i + "行");
//					
//					item = table.getItem (i);
//					
//					Set<String> qqNums = Constant.TABLE_MAPPING.keySet();
//					
//					for(String qqNum : qqNums){
//						if(Integer.parseInt(Constant.TABLE_MAPPING.get(qqNum).getIndex()) - 1 == i){
//							item.setText(new String[] { "", Constant.TABLE_MAPPING.get(qqNum).getIndex(),
//									Constant.TABLE_MAPPING.get(qqNum).getNum(), 
//									Constant.TABLE_MAPPING.get(qqNum).getPassword(),
//									Constant.TABLE_MAPPING.get(qqNum).getStatus(),
//									Constant.TABLE_MAPPING.get(qqNum).getNick(),
//									Constant.TABLE_MAPPING.get(qqNum).getLevel(), 
//									Constant.TABLE_MAPPING.get(qqNum).getOnlineDay(), 
//									Constant.TABLE_MAPPING.get(qqNum).getRemainDay(),
//									format(Constant.TABLE_MAPPING.get(qqNum).getLoginTime()), 
//									Constant.TABLE_MAPPING.get(qqNum).getOpStatus() });
//							
//							if(Constant.TABLE_MAPPING.get(qqNum).isChecked()){
//								item.setChecked(true);
//							}
//							
//							break;
//						}
//					}
//				} 
//			} 
//		};
//		
//		table.addListener (SWT.SetData, listener); 
//		table.setItemCount (COUNT); 

		
		Listener listener = new Listener() {
			@Override
			public void handleEvent(Event event) {
				TableItem item = (TableItem) event.item;
				int index = event.index;
				
				log.debug("这是第" + index + "行");
				
				Set<String> qqNums = Constant.TABLE_MAPPING.keySet();
				
				for(String qqNum : qqNums){
					if(Integer.parseInt(Constant.TABLE_MAPPING.get(qqNum).getIndex()) - 1 == index){
						item.setText(new String[] { "", Constant.TABLE_MAPPING.get(qqNum).getIndex(),
								Constant.TABLE_MAPPING.get(qqNum).getNum(), 
								Constant.TABLE_MAPPING.get(qqNum).getPassword(),
								Constant.TABLE_MAPPING.get(qqNum).getStatus(),
								Constant.TABLE_MAPPING.get(qqNum).getNick(),
								Constant.TABLE_MAPPING.get(qqNum).getLevel(), 
								Constant.TABLE_MAPPING.get(qqNum).getOnlineDay(), 
								Constant.TABLE_MAPPING.get(qqNum).getRemainDay(),
								format(Constant.TABLE_MAPPING.get(qqNum).getLoginTime()), 
								Constant.TABLE_MAPPING.get(qqNum).getOpStatus() });
						
						if(Constant.TABLE_MAPPING.get(qqNum).isChecked()){
							item.setChecked(true);
						}
						
						break;
					}
				}
			}
		};
		
		table.addListener(SWT.SetData, listener);
		
		table.setItemCount(Constant.TABLE_MAPPING.size());
		
		table.removeListener(SWT.SetData, listener);
	}

	/**
	 * 
	 * 方法概述：更新表格 
	 * 描述：更新表格
	 * 
	 * @param qqList
	 * 返回类型：void 
	 * 修改人：梁铭显 
	 * 修改时间：2012-9-8
	 */
	public void updateTable(List<QQNumVO> qqList) {
		int index = table.getItemCount();

		for (QQNumVO qqNumVO : qqList) {
			index++;

			TableVO tableVo = new TableVO();
			
			if (Constant.LOGIN_DATA.containsKey(qqNumVO.getQqNum())) {
				tableVo.setChecked(false);
				tableVo.setIndex(Integer.toString(index));
				tableVo.setNum(qqNumVO.getQqNum());
				tableVo.setPassword(isEncode ? encodePassword(qqNumVO.getPassword()) : qqNumVO.getPassword());
				tableVo.setStatus("可以免码登录");
				tableVo.setNick(qqNumVO.getNick() == null ? "" : qqNumVO.getNick());
				tableVo.setLevel(qqNumVO.getLevel() == null ? "" : qqNumVO.getLevel());
				tableVo.setOnlineDay(qqNumVO.getOnlineDate() == null ? "" : qqNumVO.getOnlineDate());
				tableVo.setRemainDay(qqNumVO.getRemainDate() == null ? "" : qqNumVO.getRemainDate());
				tableVo.setLoginTime(qqNumVO.getAlreadyLogin() == null ? (long)0 : qqNumVO.getAlreadyLogin());
				tableVo.setOpStatus("");
				
				Constant.TABLE_MAPPING.put(qqNumVO.getQqNum(), tableVo);
				
			} else {
				tableVo.setChecked(false);
				tableVo.setIndex(Integer.toString(index));
				tableVo.setNum(qqNumVO.getQqNum());
				tableVo.setPassword(isEncode ? encodePassword(qqNumVO.getPassword()) : qqNumVO.getPassword());
				tableVo.setStatus("");
				tableVo.setNick(qqNumVO.getNick() == null ? "" : qqNumVO.getNick());
				tableVo.setLevel(qqNumVO.getLevel() == null ? "" : qqNumVO.getLevel());
				tableVo.setOnlineDay(qqNumVO.getOnlineDate() == null ? "" : qqNumVO.getOnlineDate());
				tableVo.setRemainDay(qqNumVO.getRemainDate() == null ? "" : qqNumVO.getRemainDate());
				tableVo.setLoginTime(qqNumVO.getAlreadyLogin() == null ? (long)0 : qqNumVO.getAlreadyLogin());
				tableVo.setOpStatus("");
				
				Constant.TABLE_MAPPING.put(qqNumVO.getQqNum(), tableVo);
				
			}
			
			tableVo = null;
		}
		
		Listener listener = new Listener() { 
			@Override
			public void handleEvent(Event event) {
				TableItem item = (TableItem) event.item;
				int index = event.index;
				
				Set<String> qqNums = Constant.TABLE_MAPPING.keySet();
				
				for(String qqNum : qqNums){
					if(Integer.parseInt(Constant.TABLE_MAPPING.get(qqNum).getIndex()) - 1 == index){
						item.setText(new String[] { "", Constant.TABLE_MAPPING.get(qqNum).getIndex(),
								Constant.TABLE_MAPPING.get(qqNum).getNum(), 
								Constant.TABLE_MAPPING.get(qqNum).getPassword(),
								Constant.TABLE_MAPPING.get(qqNum).getStatus(),
								Constant.TABLE_MAPPING.get(qqNum).getNick(),
								Constant.TABLE_MAPPING.get(qqNum).getLevel(), 
								Constant.TABLE_MAPPING.get(qqNum).getOnlineDay(), 
								Constant.TABLE_MAPPING.get(qqNum).getRemainDay(),
								format(Constant.TABLE_MAPPING.get(qqNum).getLoginTime()), 
								Constant.TABLE_MAPPING.get(qqNum).getOpStatus() });
						break;
					}
				}
			}
		};
		
		table.addListener(SWT.SetData, listener);
		
		table.setItemCount(Constant.TABLE_MAPPING.size());
		
//		table.removeListener(SWT.SetData, listener);

//		isEncode = true;
	}

	/**
	 * 
	 * 方法概述：更新登录状态 描述：更新登录状态
	 * 
	 * @param index
	 * @param status
	 *            返回类型：void 修改人：梁铭显 修改时间：2012-9-8
	 */
	public void updateStatus(int index,String num, String status) {
		table.getItem(index).setText(4, status);
		Constant.TABLE_MAPPING.get(num).setStatus(status);
	}

	/**
	 * 
	 * 方法概述：更新操作状态 描述：更新操作状态
	 * 
	 * @param index
	 * @param status
	 *            返回类型：void 修改人：梁铭显 修改时间：2012-9-8
	 */
	public void updateOptStatus(int index, String num, String status) {
		table.getItem(index).setText(10, status);
		Constant.TABLE_MAPPING.get(num).setOpStatus(status);
	}

	/**
	 * 
	 * 方法概述：更新QQ用户名 描述：更新QQ用户名
	 * 
	 * @param index
	 * @param userName
	 *            返回类型：void 修改人：梁铭显 修改时间：2012-9-10
	 */
	public void updateUserName(int index, String num, String userName) {
		table.getItem(index).setText(5, userName);
		Constant.TABLE_MAPPING.get(num).setNick(userName);
	}

	/**
	 * 
	 * 方法概述：更新表格中的等级信息 描述：更新表格中的等级信息
	 * 
	 * @param index
	 * @param level
	 * @param days
	 * @param remainDays
	 *            返回类型：void 修改人：梁铭显 修改时间：2012-9-10
	 */
	public void updateLevelInfo(int index,String num, String level, String days, String remainDays) {
		table.getItem(index).setText(6, level);
		Constant.TABLE_MAPPING.get(num).setLevel(level);
		
		table.getItem(index).setText(7, days);
		Constant.TABLE_MAPPING.get(num).setOnlineDay(days);
		
		table.getItem(index).setText(8, remainDays);
		Constant.TABLE_MAPPING.get(num).setRemainDay(remainDays);
	}

	/**
	 * 
	 * 方法概述：更新挂机时间 描述：更新挂机时间
	 * 
	 * @param index
	 * @param loginTime
	 *            返回类型：void 修改人：梁铭显 修改时间：2012-9-11
	 */
	public void updateLoginTime(int index, String loginTime, String num, long time) {
		log.info("更新第" + index + "条：" + num);
		table.getItem(index).setText(9, loginTime);
		Constant.TABLE_MAPPING.get(num).setLoginTime(time);
	}

	/**
	 * 
	 * 方法概述：提示信息对话框 描述：提示信息对话框
	 * 
	 * @param info
	 *            返回类型：void 修改人：梁铭显 修改时间：2012-9-8
	 */
	public void alert(String info) {
		MessageBox dialog = new MessageBox(shlNiuxqq, SWT.OK | SWT.ICON_INFORMATION);
		dialog.setText("提示");
		dialog.setMessage(info);
		dialog.open();
	}

	/**
	 * 
	 * 方法概述：选择对话框 描述：选择对话框
	 * 
	 * @param info
	 *            返回类型：boolean 修改人：梁铭显 修改时间：2012-9-13
	 */
	public boolean confirm(String info) {
		MessageBox dialog = new MessageBox(shlNiuxqq, SWT.OK | SWT.CANCEL | SWT.ICON_QUESTION);
		dialog.setText("提示");
		dialog.setMessage(info);

		if (dialog.open() == SWT.OK) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 
	 * 方法概述：打开验证码输入框 描述：打开验证码输入框
	 * 
	 * @param qqNum
	 * @param imageData
	 *            返回类型：void 修改人：梁铭显 修改时间：2012-9-8
	 */
	public void showVerifyCode(String qqNum, ImageData imageData) {
		shlNiuxqq.setActive(); // 激活窗口
		shlNiuxqq.setFocus();// 没有此句可能在其他窗口下面

		verifyCodeImage.setImage(new Image(Display.getDefault(), imageData));
		verifyCode.setFocus();
		imageData = null;
		System.gc();
	}

	/**
	 * 
	 * 方法概述：把新增的QQ号码添加到列表中 描述：把新增的QQ号码添加到列表中
	 * 
	 * @param qqNum
	 * @param password
	 *            返回类型：void 修改人：梁铭显 修改时间：2012-9-14
	 */
	public void addQQToTable(String qqNum, String password) {
		if (qqNum != null && password != null && !qqNum.equals("") && !password.equals("")) {

			if (isEncode) {
				password = encodePassword(password);
			}

			int index = table.getItemCount();

			TableItem tableItem = new TableItem(table, SWT.CENTER);
			tableItem.setText(new String[] { "", Integer.toString(index + 1), qqNum, password, "",
					"", "", "", "", "" });

			QQNumVO qq = new QQNumVO();
			qq.setQqNum(qqNum);

			if (isEncode) {
				qq.setPassword(decodePassword(password));
			} else {
				qq.setPassword(password);
			}

			qq.setIndex(index + 1);

			TableVO tableVo = new TableVO();
			
			Constant.QQ_LIST.put(qq.getQqNum(), qq);
			tableVo.setChecked(false);
			tableVo.setIndex(Integer.toString(index + 1));
			tableVo.setNum(qqNum);
			tableVo.setPassword(password);
			tableVo.setStatus("");
			tableVo.setNick("");
			tableVo.setLevel("");
			tableVo.setOnlineDay("");
			tableVo.setRemainDay("");
			tableVo.setLoginTime((long)0);
			tableVo.setOpStatus("");
			Constant.TABLE_MAPPING.put(qq.getQqNum(), tableVo);

			updateQQCount(Integer.toString(Constant.QQ_LIST.size()));
		}
	}

	/**
	 * 
	 * 方法概述：密码加密 描述：密码加密 返回类型：void 修改人：梁铭显 修改时间：2012-9-12
	 */
	public String encodePassword(String password) {
		BASE64Encoder base64en = new BASE64Encoder();
		String encode = base64en.encode(password.getBytes());
		return encode;
	}

	/**
	 * 
	 * 方法概述：密码解密 描述：密码解密 返回类型：void 修改人：梁铭显 修改时间：2012-9-12
	 */
	public String decodePassword(String encode) {
		byte[] decode = "".getBytes();
		try {
			BASE64Decoder decoder = new BASE64Decoder();
			decode = decoder.decodeBuffer(encode);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new String(decode);
	}

	/**
	 * 
	 * 方法概述：设置指定的行为选择状态 描述：设置指定的行为选择状态
	 * 
	 * @param index
	 *            返回类型：void 修改人：梁铭显 修改时间：2012-9-19
	 */
	public void setSelectItem(int index) {
		table.setSelection(index);
	}

	public void updateQQCount(String count) {
		qqCount.setText(count);
	}

	public void updateAlreadyLogin(String count) {
		alreadyLogin.setText(count);
	}

	public void updateWrongPassword() {
		wrongPassword.setText(Integer.toString(Integer.parseInt(wrongPassword.getText()) + 1));
	}

	public void updateFailCount() {
		failCount.setText(Integer.toString(Integer.parseInt(failCount.getText()) + 1));
	}

	public void updateWaitingLogin(String count) {
		waitingLogin.setText(count);
	}

	/**
	 * 
	 * 方法概述：表格排序 描述：表格排序
	 * 
	 * @param columIndex
	 * @param sortStyle
	 *            返回类型：void 修改人：梁铭显 修改时间：2012-10-19
	 */
	public void sort(final int columIndex, final String sortStyle) {
		List<QQNumVO> sortList = new ArrayList<QQNumVO>();

		long startTime = System.currentTimeMillis();
		
		if (columIndex == 2) { // 按号码排序
			
			Set<String> qqNums = Constant.QQ_LIST.keySet();
			
			for(String qqNum : qqNums){
				QQNumVO tempVo = new QQNumVO();
				
				tempVo.setQqNum(Constant.QQ_LIST.get(qqNum).getQqNum());
				
				TableVO tableVo = Constant.TABLE_MAPPING.get(qqNum);
				
				tempVo.setSortElementByInteger(Integer.parseInt(tableVo.getNum()));

				sortList.add(tempVo);
			}
		} else if (columIndex == 4) {// 按登录状态排序
			Set<String> qqNums = Constant.QQ_LIST.keySet();
			
			for(String qqNum : qqNums){
				QQNumVO tempVo = new QQNumVO();
				
				tempVo.setQqNum(Constant.QQ_LIST.get(qqNum).getQqNum());
				
				TableVO tableVo = Constant.TABLE_MAPPING.get(qqNum);
				
				tempVo.setSortElementByString(tableVo.getStatus());
				
				sortList.add(tempVo);
			}
		} else if (columIndex == 6) {// 按等级排序
			Set<String> qqNums = Constant.QQ_LIST.keySet();
			
			for(String qqNum : qqNums){
				QQNumVO tempVo = new QQNumVO();
				
				tempVo.setQqNum(Constant.QQ_LIST.get(qqNum).getQqNum());
				
				TableVO tableVo = Constant.TABLE_MAPPING.get(qqNum);
				
				if(tableVo.getLevel().equals("")){
					tempVo.setSortElementByInteger(0);
				} else {
					tempVo.setSortElementByInteger(Integer.parseInt(tableVo.getLevel()));
				}
				
				sortList.add(tempVo);
			}
		} else if (columIndex == 7) {// 按活跃天数排序
			Set<String> qqNums = Constant.QQ_LIST.keySet();
			
			for(String qqNum : qqNums){
				QQNumVO tempVo = new QQNumVO();
				
				tempVo.setQqNum(Constant.QQ_LIST.get(qqNum).getQqNum());
				
				TableVO tableVo = Constant.TABLE_MAPPING.get(qqNum);
				
				if(tableVo.getOnlineDay().equals("")){
					tempVo.setSortElementByInteger(0);
				} else {
					tempVo.setSortElementByInteger(Integer.parseInt(tableVo.getOnlineDay()));
				}
				
				sortList.add(tempVo);
			}
		} else if (columIndex == 8) {// 按剩余天数排序
			Set<String> qqNums = Constant.QQ_LIST.keySet();
			
			for(String qqNum : qqNums){
				QQNumVO tempVo = new QQNumVO();
				
				tempVo.setQqNum(Constant.QQ_LIST.get(qqNum).getQqNum());
				
				TableVO tableVo = Constant.TABLE_MAPPING.get(qqNum);
				
				if(tableVo.getRemainDay().equals("")){
					tempVo.setSortElementByInteger(0);
				} else {
					tempVo.setSortElementByInteger(Integer.parseInt(tableVo.getRemainDay()));
				}
				
				sortList.add(tempVo);
			}
		} else if (columIndex == 9) {// 按挂机时间排序
			Set<String> qqNums = Constant.QQ_LIST.keySet();
			
			for(String qqNum : qqNums){
				QQNumVO tempVo = new QQNumVO();
				
				tempVo.setQqNum(Constant.QQ_LIST.get(qqNum).getQqNum());
				
				TableVO tableVo = Constant.TABLE_MAPPING.get(qqNum);
				
				if(tableVo.getLoginTime() == 0){
					tempVo.setSortElementByInteger(0);
				} else {
					tempVo.setSortElementByInteger(Integer.parseInt(Long.toString(tableVo.getLoginTime())));
				}
				
				sortList.add(tempVo);
			}
		}

		long setSortList = System.currentTimeMillis();
		log.debug("设置排序集合所用的时间是：" + (setSortList - startTime) / 1000 + "秒");
		
		long startSortList = System.currentTimeMillis();
		
		try{
			// 使用JAVA6的排序方法
			System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");
			
			// 排序
			Collections.sort(sortList, new Comparator<QQNumVO>() {
				@Override
				public int compare(QQNumVO o1, QQNumVO o2) {
					if (columIndex == 4) {
						if (sortStyle.equals("ASC")) {
							return o1.getSortElementByString().compareTo(o2.getSortElementByString());
						} else {
							return o2.getSortElementByString().compareTo(o1.getSortElementByString());
						}
					} else {
						if (sortStyle.equals("ASC")) {
							return o1.getSortElementByInteger() > o2.getSortElementByInteger() ? 1 : -1;
						} else {
							return o1.getSortElementByInteger() < o2.getSortElementByInteger() ? 1 : -1;
						}
					}
				}
			});
		} catch (Exception e) {
			alert("排序出错！");
			log.error(e);
		}
		
		long sortEndTime = System.currentTimeMillis();
		
		log.debug("排序所用的时间是：" + (sortEndTime - startSortList) / 1000 + "秒");
		
//		long updateTableStartTime = System.currentTimeMillis();
//		
//		//获取原数据
//		final List<String[]> tempList = new ArrayList<String[]>();
//		
//		Set<String> keySet = Constant.TABLE_MAPPING.keySet();
//		
//		for(String s : keySet){
//			String[] tempArray = new String[10];
//			
//			tempArray[0] = Constant.TABLE_MAPPING.get(s).getIndex();
//			tempArray[1] = Constant.TABLE_MAPPING.get(s).getNum();
//			tempArray[2] = Constant.TABLE_MAPPING.get(s).getPassword();
//			tempArray[3] = Constant.TABLE_MAPPING.get(s).getStatus();
//			tempArray[4] = Constant.TABLE_MAPPING.get(s).getNick();
//			tempArray[5] = Constant.TABLE_MAPPING.get(s).getLevel();
//			tempArray[6] = Constant.TABLE_MAPPING.get(s).getOnlineDay();
//			tempArray[7] = Constant.TABLE_MAPPING.get(s).getRemainDay();
//			tempArray[8] = format(Constant.TABLE_MAPPING.get(s).getLoginTime());
//			tempArray[9] = Constant.TABLE_MAPPING.get(s).getOpStatus();
//
//			tempList.add(tempArray);
//		}
//		
//		log.debug("原数据有" + tempList.size() + "条");
		
//		long getOldDataTime = System.currentTimeMillis();
//		
//		log.debug("获取原数据所用的时间是：" + (getOldDataTime - updateTableStartTime) / 1000 + "秒");
		
		table.removeAll(); // 清空表格
		
		for (int i = 0; i < sortList.size(); i++) {
			Constant.QQ_LIST.get(sortList.get(i).getQqNum()).setIndex(i);// 设置新的序号

			//设置新的数据映射
			Constant.TABLE_MAPPING.get(sortList.get(i).getQqNum()).setIndex(Integer.toString(i+1));
		}
		
		log.debug("表映射对象有" + Constant.TABLE_MAPPING.size() + "条");
		
		long startInsertDataTime = System.currentTimeMillis();
		
		updateTableNO();
//		table.addListener(SWT.SetData, new Listener() {
//			@Override
//			public void handleEvent(Event event) {
//				TableItem item = (TableItem) event.item;
//				int index = event.index;
//				
//				log.debug("这是第" + index + "行");
//				
//				TableVO tableVo = new TableVO();
//				
//				Set<String> qqNums = Constant.TABLE_MAPPING.keySet();
//				
//				for(String qqNum : qqNums){
//					if(Integer.parseInt(Constant.TABLE_MAPPING.get(qqNum).getIndex()) - 1 == index){
//						tableVo = Constant.TABLE_MAPPING.get(qqNum);
//						break;
//					}
//				}
//				
//				log.debug("这一行的号码是：" + tableVo.getNum());
//				
//				for (int j = 0; j < tempList.size(); j++) {
//					if (tempList.get(j)[1].equals(tableVo.getNum())) {
//						log.debug("这一行是：" + tableVo.getIndex() + "---" + tableVo.getNum());
//						
//						item.setText(new String[] { "", tableVo.getIndex(),
//								tempList.get(j)[1], tempList.get(j)[2], tempList.get(j)[3],
//								tempList.get(j)[4], tempList.get(j)[5], tempList.get(j)[6],
//								tempList.get(j)[7], tempList.get(j)[8], tempList.get(j)[9] });
//						break;
//					}
//				}
//			}
//		});
//		
//		table.setItemCount(Constant.TABLE_MAPPING.size());
		
		long updateTableEndTime = System.currentTimeMillis();
		
		log.debug("更新表格所用的时间是：" + (updateTableEndTime - startInsertDataTime) / 1000 + "秒");
		log.debug("排序所用的总时间是：" + (updateTableEndTime - startTime) / 1000 + "秒");
	}

	/**
	 * 
	 * 方法概述：修改登录密码
	 * 描述：修改登录密码
	 * @param qqNum
	 * 返回类型：void
	 * 修改人：梁铭显
	 * 修改时间：2012-11-21
	 */
	public void updatePassword(String qqNum) {
		QQNumVO qq = Constant.QQ_LIST.get(qqNum);
		TableItem item = table.getItem(qq.getIndex());

		if (isEncode) {
			item.setText(3, encodePassword(qq.getPassword()));
			Constant.TABLE_MAPPING.get(qqNum).setPassword(encodePassword(qq.getPassword()));
		} else {
			item.setText(3, qq.getPassword());
			Constant.TABLE_MAPPING.get(qqNum).setPassword(qq.getPassword());
		}
	}

	/**
	 * 方法概述：检查COOKIES数据是否有效 描述：检查COOKIES数据是否有效
	 * 
	 * @param columIndex
	 * @param sortStyle
	 *            返回类型：void 修改人：梁铭显 修改时间：2012-10-23
	 */
	@SuppressWarnings("deprecation")
	public void checkVerifyCode() {
		// 发送判断是否需要验证码的请求
		// 创建默认的httpClient实例.
		HttpParams params = new BasicHttpParams();

		// 连接超时 100秒
		params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, Constant.TIMEOUT);

		// 读取超时 100秒
		params.setParameter(CoreConnectionPNames.SO_TIMEOUT, Constant.TIMEOUT);

		params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
		
		HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

		SchemeRegistry schemeRegistry = new SchemeRegistry();

		// 连接方式
		schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));

		PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);

		cm.setMaxTotal(Constant.MAX_CONNECTION);// 最大连接数

		HttpClient httpclient = new DefaultHttpClient(cm, params);

		// 第二次登录
		HttpPost httppost = new HttpPost("http://d.web2.qq.com/channel/login2");

		// 设置http头信息
		httppost.setHeader("Accept", "*/*");
		httppost.setHeader("Accept-Encoding", "gzip,deflate,sdch");
		httppost.setHeader("Accept-Language", "zh-CN,zh;q=0.8");
		httppost.setHeader("Connection", "Keep-Alive");
		httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
		httppost.setHeader("Host", "d.web2.qq.com");
		httppost.setHeader("Referer",
				"http://d.web2.qq.com/proxy.html?v=20110331002&callback=1&id=2"); // 一定要这一个，如果不是会出现103错误
		httppost.setHeader("User-Agent",
				"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");

		String ptwebqq = "";

		String qq = Constant.LOGIN_DATA.keySet().iterator().next();

		List<Cookie> cookies = Constant.LOGIN_DATA.get(qq);

		// 从cookies中获取ptwebqq的值
		for (int i = 0; i < cookies.size(); i++) {
			if (cookies.get(i).getName().equals("ptwebqq")) {
				ptwebqq = cookies.get(i).getValue();
			}
		}

		for (Cookie cookie : cookies) {
			((AbstractHttpClient) httpclient).getCookieStore().addCookie(cookie);
		}

		String param = "{\"status\":\""
				+ Constant.LOGIN_STATUS
				+ "\",\"ptwebqq\":\""
				+ ptwebqq
				+ "\","
				+ "\"passwd_sig\":\"\",\"clientid\":\"50705043\",\"psessionid\":null}";

		String encode = URLEncoder.encode(param);

		String responseString = "";
		try {
			StringEntity stringEntity = new StringEntity("r=" + encode
					+ "&clientid=50705043&psessionid=null");
			stringEntity.setContentType("application/x-www-form-urlencoded");

			httppost.setEntity(stringEntity);

			HttpResponse response = httpclient.execute(httppost);

			// 获取响应实
			HttpEntity entity = response.getEntity();

			responseString = EntityUtils.toString(entity);
		} catch (IOException e) {
			log.error("网络连接超时", e);
			e.printStackTrace();
			responseString = "";
		}

		log.debug(responseString);

		if (!responseString.contains("{\"retcode\":0")) { // Cookies过期
			log.debug("数据过期");
			// 清空登录数据
			Constant.LOGIN_DATA.clear();
		}
	}

	/**
	 * 方法概述：选取列表中的QQ（定时登录时执行） 描述：选取列表中的QQ（定时登录时执行）
	 * 
	 * @param 返回类型
	 *            ：List<QQNumVO> 修改人：梁铭显 修改时间：2012-11-05
	 */
	public List<QQNumVO> getAllQQ() {
		List<QQNumVO> qqList = new ArrayList<QQNumVO>();

		Set<String> keySet = Constant.TABLE_MAPPING.keySet();
		
		for (String s : keySet) {
			Constant.TABLE_MAPPING.get(s).setChecked(true);
			
			QQNumVO qqNumVO = Constant.QQ_LIST.get(s); // 从选择的行中取得QQ号码
			
			// 如果已经登录就过滤掉
			if (qqNumVO.isLogin()) {
				continue;
			}

			qqNumVO.setIndex(Integer.parseInt(Constant.TABLE_MAPPING.get(s).getIndex()) - 1);
			qqList.add(qqNumVO);
		}
		
//		for (int i = 0; i < table.getItemCount(); i++) {
//
//			final int index = i;
//
//			table.getItem(index).setChecked(true);
//
//			if (table.getItem(i).getChecked()) {
//				QQNumVO qqNumVO = Constant.QQ_LIST.get(table.getItem(i).getText(2)); // 从选择的行中取得QQ号码
//
//				// 如果已经登录就过滤掉
//				if (qqNumVO.isLogin()) {
//					continue;
//				}
//
//				qqNumVO.setIndex(i);
//				qqList.add(qqNumVO);
//			}
//		}

		return qqList;
	}
}
